<template>
  <view class="ranking-list" v-if="currentNav.list.length > 3">
    <view class="header">
      <text :class="['sub-title', nav.id === currentNav.id ? 'title' : '']" v-for="nav in navList" :key="nav.id"
        @tap="switchNav(nav)">{{ nav.title }}</text>
    </view>

    <!-- 前三名展示 -->
    <view class="top-three">
      <!-- 第二名 -->
      <view class="rank-item second">
        <view class="avatar-box">
          <image class="rank-icon" :src="baseImageUrl + 'test-yujia/index/top-icon-2.png'" mode="aspectFit" />
          <image class="avatar" :src="currentNav.list[1].avatar" mode="aspectFill" />
          <view class="rank-tag">NO.2</view>
        </view>
        <view class="info">
          <text class="name">{{ currentNav.list[1].name }}</text>
          <text class="score">{{ currentNav.list[1].score }}</text>
        </view>
      </view>

      <!-- 第一名 -->
      <view class="rank-item first">
        <view class="avatar-box">
          <image class="rank-icon" :src="baseImageUrl + 'test-yujia/index/top-icon-1.png'" mode="aspectFit" />
          <image class="avatar" :src="currentNav.list[0].avatar" mode="aspectFill" />
          <view class="rank-tag">NO.1</view>
        </view>
        <view class="info">
          <text class="name">{{ currentNav.list[0].name }}</text>
          <text class="score">{{ currentNav.list[0].score }}</text>
        </view>
      </view>

      <!-- 第三名 -->
      <view class="rank-item third">
        <view class="avatar-box">
          <image class="avatar" :src="currentNav.list[2].avatar" mode="aspectFill" />
          <view class="rank-tag">NO.3</view>
          <image class="rank-icon" :src="baseImageUrl + 'test-yujia/index/top-icon-3.png'" mode="aspectFit" />
        </view>
        <view class="info">
          <text class="name">{{ currentNav.list[2].name }}</text>
          <text class="score">{{ currentNav.list[2].score }}</text>
        </view>
      </view>
    </view>

    <!-- 其他排名列表 -->
    <view class="other-ranks">
      <view class="rank-row" v-for="(item, index) in currentNav.list.slice(3)" :key="index">
        <text class="rank-number">{{ index + 4 }}</text>
        <image class="avatar" :src="item.avatar" mode="aspectFill" />
        <text class="name">{{ item?.name }}</text>
        <view class="course-info" v-if="currentNav.id === 'user'">
          <text class="label">上课</text>
          <text class="value">{{ item.courseCount }}节</text>
        </view>
        <view class="points-info" v-if="currentNav.id === 'user'">
          <text class="label">奖励积分</text>
          <text class="value">{{ item.points }}</text>
        </view>
        <view class="course-info" v-if="currentNav.id === 'teacher'">
          <text class="label">科目</text>
          <text class="value">{{ item.courseCount }}</text>
        </view>
        <view class="points-info" v-if="currentNav.id === 'teacher'">
          <text class="label">累计开课</text>
          <text class="value">{{ item.points }}节</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { onMounted, ref } from "vue";
import { useCommonStore } from "@/store/index";
import { getTeacherRankAPI, getUserRankAPI } from "@/apis/home";

const commonStore = useCommonStore();
const baseImageUrl = commonStore.baseImageUrl;

const rankList = ref([
  {
    name: "赵同学",
    score: 30,
    avatar: "/static/images/avatar.jpg",
    courseCount: 10,
    points: 1000,
  },
 
]);

const teacherRankList = [
 
  {
    name: "王老师",
    score: 30,
    avatar: "/static/images/avatar.jpg",
    courseCount: 10,
    points: 1000,
  },

];

const userRankList = [
  {
    name: "张同学",
    score: 88,
    avatar: "/static/images/avatar.jpg",
    courseCount: 10,
    points: 1000,
  }
];
const navList = [
  { id: "user", title: "上课总排行", list: userRankList },
  { id: "teacher", title: "课程总排行", list: teacherRankList },
];


const currentNav = ref(navList[0]);
console.log("currentNav", currentNav.value);


const switchNav = (nav) => {
  currentNav.value = nav;
  console.log("currentNav", currentNav.value);
}

onMounted(async () => {
  // 获取教练排行榜
  const { code, data } = await getTeacherRankAPI({ branchCode: commonStore.storeAndCityInfo.branchCode });
  console.log("教练排行榜", data);
  teacherRankList.value = data;

  // 获取用户排行榜
  const userRankRes = await getUserRankAPI({ branchCode: commonStore.storeAndCityInfo.branchCode });
  console.log("用户排行榜", userRankRes.data);
  userRankList.value = userRankRes.data;
});
</script>

<style lang="scss">
.ranking-list {
  padding: 30rpx;
  margin-top: 20rpx;

  .avatar-box {
    position: relative;

    .rank-icon {
      width: 40rpx;
      height: 40rpx;
      position: absolute;
      right: -10rpx;
      top: 4rpx;
      z-index: 0;
    }

    .avatar {
      width: 120rpx;
      height: 120rpx;
      border-radius: 50%;
      border: 4rpx solid #fff;
      box-shadow: 0 4rpx 8rpx rgba(0, 0, 0, 0.1);
      z-index: 2;
      background-color: rebeccapurple;
      position: relative;
    }

    .rank-tag {
      background-color: #b0c56f;
      font-size: 20rpx;
      font-weight: bold;
      color: #222222;
      position: absolute;
      left: 50%;
      bottom: 4rpx;
      z-index: 3;
      transform: translateX(-50%) skew(-16deg);
      padding: 0 6rpx;
    }
  }

  .header {
    display: flex;
    align-items: center;
    gap: 90rpx;
    justify-content: center;

    .sub-title {
      font-size: 30rpx;
      color: #666;
      background: #fff;
      padding: 8rpx 20rpx;
      border-radius: 40rpx;

      &.title {
        font-weight: bold;
        color: #333;
        background: #60702d;
        color: #fff;
      }
    }




  }

  .top-three {
    display: flex;
    justify-content: center;
    align-items: flex-end;
    margin-bottom: 40rpx;
    padding: 0 20rpx;
    margin-top: 60rpx;

    .rank-item {
      position: relative;
      display: flex;
      flex-direction: column;
      align-items: center;
      width: 33.33%;

      &.first {
        top: -24rpx;
      }

      &.third {
        top: 14rpx;
      }

      .crown {
        position: absolute;
        top: -30rpx;
        width: 40rpx;
        height: 40rpx;
      }
    }

    .info {
      text-align: center;
      margin-top: 10rpx;

      .name {
        font-size: 30rpx;
        color: #0f0f0f;
        display: block;
      }

      .score {
        font-size: 22rpx;
        color: #0f0f0f;
      }
    }
  }

  .other-ranks {
    background: #fff;
    border-radius: 20rpx;
    padding: 20rpx;

    .rank-row {
      display: flex;
      align-items: center;
      padding: 36rpx 34rpx;
      border-bottom: 1rpx solid #f5f5f5;
      box-sizing: border-box;

      &:last-child {
        border-bottom: none;
      }

      .rank-number {
        text-align: center;
        font-size: 22rpx;
        color: #0f0f0f;
        margin-right: 26rpx;
      }

      .avatar {
        width: 74rpx;
        height: 74rpx;
        border-radius: 50%;
        margin: 0 20rpx;
        background: #eee;
      }

      .name {
        flex: 1;
        font-weight: 500;
        font-size: 24rpx;
        color: #0f0f0f;
      }

      .course-info,
      .points-info {
        margin-left: 20rpx;
        text-align: right;

        .label {
          font-size: 18rpx;
          color: #898989;
          margin-right: 10rpx;
        }

        .value {
          font-size: 20rpx;
          color: #0e0e0e;
          font-weight: bold;
        }
      }
    }
  }
}
</style>